#include "filemanagement.h"

void getFileRec_FileManagement(List *list, const char *location, const char *sub)
{
  GDir *folder;
  const gchar* file = NULL;
  char * f = NULL;
  f = g_build_filename(location, sub, NULL);
      
  if(g_file_test(f, G_FILE_TEST_IS_DIR) == TRUE)
    {
      
      folder  = g_dir_open(f, 0, NULL);
      while((file = g_dir_read_name(folder)) != NULL)
	{
	  char *fsubt = NULL;
	  char *fsub = NULL;
	  fsub = g_build_filename(sub, file, NULL);
	  fsubt = g_build_filename(location, sub, file, NULL);
	  if(g_file_test(fsubt, G_FILE_TEST_IS_DIR) == FALSE)
	    {
	      insertLast_List(list, (Element)fsub);
	    }
	  else
	    getFileRec_FileManagement(list, location, fsub);
	  g_free(fsub);
	  g_free(fsubt);
	}
      g_dir_close(folder);
    }
  else
    {
      fprintf(stderr, "[getFileRec_FileManagement]Erreur sur le fichier/dossier %s\n", f);
    }
  g_free(f);
}

void getFile_FileManagement(List *list, const char *location)
{
  GDir *folder;
  const gchar* file = NULL;

  if(g_file_test(location, G_FILE_TEST_IS_DIR) == TRUE)
    {
      folder  = g_dir_open(location, 0, NULL);
      initialize_List(list, create_CharList, delete_CharList, compare_CharList, NULL);

      while((file = g_dir_read_name(folder)) != NULL)
	{
	  char *fsub = NULL;
	  fsub = g_build_filename(location, file, NULL);
	  if(g_file_test(fsub, G_FILE_TEST_IS_DIR) == FALSE)
	    insertLast_List(list, (Element)file);
	  else
	    getFileRec_FileManagement(list, location, file);
	  g_free(fsub);
	}
      g_dir_close(folder);
    }
  else
    {
      fprintf(stderr, "[getFile_FileManagement]Erreur sur le fichier/dossier %s\n", location);
    }
}
